<?php
/*
 * @Author: 逅来
 * @Description: 
 * @Date: 2023-03-19 14:55:20
 * @LastEditTime: 2023-03-29 16:58:59
 * @FilePath: \zhaopian.com\application\api\controller\photo\Login.php
 * @email: 934749708@qq.com
 */



namespace app\api\controller\photo;

use Think\Db;
use app\common\controller\Api;
use app\admin\model\photo\Account as AccountModel;
use app\admin\model\photo\User as UserModel;
use app\admin\model\photo\Information as InformationModel;
use app\api\controller\Validate;
use app\admin\model\photo\Itinerary as ItineraryModel;
use app\admin\model\photo\Related as RelatedModel;
use app\admin\model\photo\File as FileModel;
use app\admin\model\photo\Phone as PhoneModel;

class Login extends Api
{
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
        $this->Itinerary = new \app\admin\model\photo\Itinerary;
    }
    public function img()
    {
        $data = $this->request->param('');
        $res = FileModel::get($data['id']);
        if ($res) {
            return $this->succ_ess(200, '图片详情', 'type', 1, $res);
        }
    }
    //绑定手机判断身份
    public function bangding(){
        $data = $this->request->param('');
        $res = UserModel::where('open_id', $data['openid'])->find();
        $usermodel = UserModel::get($res->id);
     
        $usermodel->phone = $data['phone'];
        $res = $usermodel->save();
        if($res){
            return $this->succ_ess(200, '绑定手机判断身份', 'type', 1, $res);
        }
    }

    public function succ_ess($number, $text, $title, $list, $data)
    {
        return json(['code' => $number, 'msg' => $text, $title => $list, 'data' => $data]);
    }
    // 游客 我的行程
    public function myItinerary()
    {
        $data = $this->request->param('');
     
        $id = implode(',', array_column(Db::table('fa_photo_bind')->where('user_id', $data['user_id'])->group('itinerary_id')->field('itinerary_id')->select(), 'itinerary_id'));
      
        $list = $this->Itinerary->with(['route'])->whereIn('id', $id)->select();
        foreach ($list as $k => &$v) {
            // $v['route_city'] = Db::table('fa_photo_city')->whereIn('id', $v['route']['city_id'])->field('name')->select();
            $v['route_city'] = implode("->",array_column(Db::table('fa_photo_city')->whereIn('id', $v['route']['city_id'])->field('name')->select(),'name'));
            $v['file_count'] = RelatedModel::where('itinerary_id', $v['id'])->count();
        }
        if ($list) {

            return $this->succ_ess(200, '行程列表', 'type', 1, $list);
        } else {
            return $this->succ_ess(200, '暂无行程', 'type', 0, $list);
        }
    }
    /**
     * @description: 行程列表
     * @return {*}
     * @author: 逅来
     */
    public function Itinerary()
    {
        $data = $this->request->param('');
        // $Validate = new Validate();
        // $Validate->Itinerary($data);
        // $Itinerary = ItineraryModel::where('account_id',$data['user_id'])->select();
        $Itinerary = $this->Itinerary->with(['route'])->where('account_id',$data['user_id'])->select();
        foreach ($Itinerary as $k => &$v) {
            $v['file_count'] = RelatedModel::where('itinerary_id', $v['id'])->count();
            $v['number_count'] = RelatedModel::where('itinerary_id', $v['id'])->group('user_id')->count();
            $v['route_city'] = implode("->",array_column(Db::table('fa_photo_city')->whereIn('id', $v['route']['city_id'])->field('name')->select(),'name'));
        }

        if ($Itinerary) {

            return $this->succ_ess(200, '行程列表', 'type', 1, $Itinerary);
        } else {
            return $this->succ_ess(200, '暂无行程', 'type', 0, $Itinerary);
        }
    }
    /**
     * @description: 摄影师登录
     * @return {*}
     * @author: 逅来
     */
    public function login()
    {

        $user = $this->request->param('user'); //客户id
        $pass = $this->request->param('pass'); //客户id

        $usermodel = AccountModel::where('user', $user)->where('pass', $pass)->find();

        $res = array();
        if ($usermodel) {

            $token = $this->create_token();
            $usermodel = AccountModel::get($usermodel->id);
            $usermodel->token = $token;
            $usermodel->Expiration_time = time() + 300;;

            $usermodel->save();
            $res['token'] = $token;
            $res['account_id'] = $usermodel->id;
            return $this->succ_ess(200, '摄影师登录成功', 'type', 1, $res);
        } else {
            return $this->succ_ess(400, '请输入正确的账号密码', 'type', 0, '');
        }
    }
    function create_token()
    {
        $str = md5(uniqid(md5(microtime(true)), true)); //生成一个不会重复的字符串
        $str = sha1($str); //加密
        return $str;
    }

    // public function GetAPhoneNumber()
    // {
    //     $data = $this->request->param(); //id       
    //     $code = $data['code'];
    //     $url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=$code";
    //     $a = $this->curl_get($url);
    //     $data = (array)json_decode($a);
    //     dump($data);
    // }

    public function GetAPhoneNumber()
    {
          $data = $this->request->param(); //id       
       
        $code = $data['code'];
        $appid = 'wx246e02bf9d168c6f';
        $secret = '9a4c64ccac178695638bf50fbdacdda3';

        $url_get = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $appid . '&secret=' . $secret;
        $tmptoken = json_decode($this->curl_get($url_get), true);

        $token = $tmptoken['access_token'];

        $url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=" . $token;
        $a = ['code'=>$code];
     
        $info = $this->Post(json_encode($a), $url);
        $tmpinfo = json_decode($info, true);
      
   


        $phone = $tmpinfo['phone_info']['phoneNumber'];

        return $this->succ_ess(200, '获取手机号成功', 'type', 1, $phone);
    }

    function Post($curlPost, $url, $ssl = false)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_NOBODY, true);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
        if (!$ssl) {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        }
        $return_str = curl_exec($curl);
        curl_close($curl);
        return $return_str;
    }
    // 公司介绍和联系我们
    public function Information()
    {
        $data = $this->request->param('');
        $InformationModel = InformationModel::where('id', 1)->find();

        switch ($data['type']) {
            case 1:
                $title = '首页图片';
                $res = 'https://zhangluxiaochengxu.oss-cn-qingdao.aliyuncs.com'.$InformationModel->image;
                break;
            case 2:
                $title = '公司介绍';
                $res = $InformationModel->content;
                break;
            case 3:
                $title = '联系我们';
                $res = $InformationModel->contents;
                break;
        }
        return $this->succ_ess(200, $title, 'type', 1, $res);
    }

    /**
     * @description: 小程序登录 
     * @return {*}
     * @author: 逅来
     */
    public function xcxLogin()
    {
        $code = $this->request->param('code');
        $nickname = $this->request->param('nickname');
        $avatar = $this->request->param('avatar');
        
        $status = $this->request->param('status');
        $appid = 'wx246e02bf9d168c6f';
        $secret = '9a4c64ccac178695638bf50fbdacdda3';
        // dump($appid);
        // dump($secret);die; 
        $url = "https://api.weixin.qq.com/sns/jscode2session?appid=$appid&secret=$secret&js_code=$code&grant_type=authorization_code";
        $a = $this->curl_get($url);
        $data = (array)json_decode($a);
        $token = $this->create_token();

        $res = UserModel::where('open_id', $data['openid'])->find();
       
        if (!$res) {
          
            $usermodel = new UserModel();
            $usermodel->open_id = $data['openid'];
            $usermodel->status = 1;
            $usermodel->token =  $token;
            $usermodel->headerimg = $avatar;
            $usermodel->nickname =  $nickname;
            $usermodel->Expiration_time = time() + 300000;
            $usermodel->save();
            $user['token'] = $token;
            $user['user_id'] = $usermodel->id;
            $user['identity'] = $usermodel->status;
            $user['nickname'] = $usermodel->nickname;
            $user['headerimg'] = $usermodel->headerimg;
            return $this->succ_ess(200, '登录成功', 'type', 1, $user);
        } else {
           
            $usermodel = UserModel::get($res->id);
            $usermodel->token =  $token;
            $usermodel->headerimg = $avatar;
            $usermodel->nickname =  $nickname;
            $usermodel->Expiration_time = time() + 300000;
            $usermodel->save();
            $user['token'] = $token;
            $user['user_id'] = $usermodel->id;
            $user['identity'] = $usermodel->status;
            $user['nickname'] = $usermodel->nickname;
            $user['headerimg'] = $usermodel->headerimg;
            return $this->succ_ess(200, '登录成功', 'type', 1, $user);
        }
    }

    public function UserInfo(){
         $nickname = $this->request->param('nickname');
        $avatar = $this->request->param('avatar');
        $user_id = $this->request->param('user_id');
        $usermodel = UserModel::get($user_id);
            $usermodel->nickname =  $nickname;
            $usermodel->headerimg = $avatar;
            $usermodel->save();
            $user['identity'] = $usermodel->status;
            $user['nickname'] = $usermodel->nickname;
            $user['headerimg'] = $usermodel->headerimg;
            return $this->succ_ess(200, '授权成功', 'type', 1, $user);
    }

    /**
     * @description: 小程序登录 
     * @return {*}
     * @author: 逅来
     */
    public function xcxLogin1()
    {
        $code = $this->request->param('code');
        $nickname = $this->request->param('nickname');
        $avatar = $this->request->param('avatar');
      
        $status = $this->request->param('status');
        $appid = 'wx246e02bf9d168c6f';
        $secret = '9a4c64ccac178695638bf50fbdacdda3';
        // dump($appid);
        // dump($secret);die; 
        $url = "https://api.weixin.qq.com/sns/jscode2session?appid=$appid&secret=$secret&js_code=$code&grant_type=authorization_code";
        $a = $this->curl_get($url);
        $data = (array)json_decode($a);
        $token = $this->create_token();

        $res = UserModel::where('open_id', $data['openid'])->find();
        if ($res) {
           
            $usermodel = UserModel::get($res->id);
            $user['token'] = $usermodel->token;
            $user['user_id'] = $usermodel->id;
            $user['identity'] = $usermodel->status;
            $user['nickname'] = $usermodel->nickname;
            $user['headerimg'] = $usermodel->headerimg;
            
            return $this->succ_ess(200, '登录成功', 'type', 1, $user);
        }else{
              return $this->succ_ess(200, '暂无授权', 'type', 1, '');
        }
        
    }

    public function curl_get($url)
    {

        $header = array(
            'Accept: application/json',
        );
        $curl = curl_init();
        //设置抓取的url
        curl_setopt($curl, CURLOPT_URL, $url);
        //设置头文件的信息作为数据流输出
        curl_setopt($curl, CURLOPT_HEADER, 0);
        // 超时设置,以秒为单位
        curl_setopt($curl, CURLOPT_TIMEOUT, 1);

        // 超时设置，以毫秒为单位
        // curl_setopt($curl, CURLOPT_TIMEOUT_MS, 500);

        // 设置请求头
        curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
        //设置获取的信息以文件流的形式返回，而不是直接输出。
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        //执行命令
        $data = curl_exec($curl);
        curl_close($curl);
        return $data;
    }
}
